﻿using System;
using System.IO;
using System.Collections;
using System.Globalization;
using System.Xml;

namespace SMSTasker
{
    class Localizer
    {
        private XmlDocument doc;

        private static Localizer instance;

        private Localizer()
        {
        }

        public static Localizer Instance
        {
          get 
          {
             if (instance == null)
             {
                 instance = new Localizer();
             }
             return instance;
          }
        }

        public static String GetLocalizationFilePath()
        {
            String locale = CultureInfo.CurrentCulture.Name;
            string currentDirectory = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;
            currentDirectory = currentDirectory.Substring(0, currentDirectory.LastIndexOf("\\") + 1);
            String fileName = "";
            if (locale.IndexOf("en-") == 0)
            {
                fileName = currentDirectory + "en-EN.xml";
            }
            else
            {
                fileName = currentDirectory + locale + ".xml";
            }

            if (!File.Exists(fileName))
            {
                fileName = currentDirectory + "en-EN.xml";
                if (!File.Exists(fileName))
                {
                    throw new Exception("Error getting localized strings for you language!");
                }
            }

            return fileName;
        }

        private void LoadDocument()
        {
            doc = new XmlDocument();
            String locale = CultureInfo.CurrentCulture.Name;
            string currentDirectory = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;
            currentDirectory = currentDirectory.Substring(0, currentDirectory.LastIndexOf("\\") + 1);
            String fileName = "";
            if (locale.IndexOf("en-") == 0)
            {
                fileName = currentDirectory + "en-EN.xml";
            }
            else
            {
                fileName = currentDirectory + locale + ".xml";
            }
            
            if (File.Exists(fileName))
            {
                doc.Load(fileName);
            }
            else
            {
                fileName = currentDirectory + "en-EN.xml";
                if (File.Exists(fileName))
                {
                    doc.Load(fileName);
                }
                else
                {
                    throw new Exception("Error getting localized strings for you language!");
                }
            }
        }
        
        public String GetString(String ItemName)
        {
            if (doc == null)
            {
                LoadDocument();   
            }
            String ret = "";

            try
            {
                XmlNode root = doc.DocumentElement;
                IEnumerator ienum = root.GetEnumerator();

                while (ienum.MoveNext())
                {
                    if (((XmlNode)ienum.Current).Attributes["id"].Value.ToLower() == ItemName.ToLower())
                    {
                        ret = ((XmlNode)ienum.Current).InnerText;
                        break;
                    }
                }
            }
            catch
            {
            	
            }
            
            return ret;
        }
    }
}
